package cn.wu.reggie.interceptor;

import cn.wu.reggie.common.R;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginCheckInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1 获取session中的id值
        Object employee = request.getSession().getAttribute("employee");
        Object user = request.getSession().getAttribute("user");

        // 2 判断employee是否为空 如果为空 拦截返回响应数据
        if (user==null && employee == null){
            response.getWriter().print(JSON.toJSON(R.error("NOTLOGIN")));
            return false;
        }

        // 3 若不为空放行
        return true;

    }
}
